<?php

class Newsletter extends MX_Controller {

    function Newsletter() {
        parent::__construct();

        $this->load->model('subscribermodel');
        $this->load->model('email/emailmodel');
        $this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
    }

    function index() {
        redirect('subscribers/local/verified/1');
    }

    function subscribe($type = 'local') {
        $data = "";
        //load language
        $this->lang->load('common/header', $this->language);
        $this->lang->load('newsletter/newsletter', $this->language);
        //get global lable

        if ($this->input->post('action') == 'subscribed') {

            $data['first_name'] = $this->input->post('firstname');
            $data['last_name'] = $this->input->post('lastname');
            $data['email'] = $this->input->post('email');
            $data['verification_code'] = random_string('alnum', 16);
            $data['verified'] = '0';
            $data['status'] = '1';
            $data['date_code_generated'] = date('d-M-Y G:i:s');
            $data['date_verified'] = NULL;
            $data['list_type'] = $type;
            $data['market'] = $this->config->item('market');

            if ($type == 'local') {
                $form = $this->emailmodel->get_admin_email(6); // get local newsletter admin email
            } else {
                $form = $this->emailmodel->get_admin_email(7); // get local newsletter admin email
            }

            $to_admin = $form->AdminAddress;

            if ($data['list_type'] != 'all') {

                if (!$this->subscribermodel->checkexists($data)) {

                    $this->subscribermodel->addsubscriber($data);

                    //send email
                    $to = $data['email'];

                    $from = $this->lang->line('text_from_subscribe_local');
                    $bcc = "";
                    $subject = $this->lang->line('text_subject_subscribe_local') . $data['first_name'];

                    $text = sprintf($this->lang->line('text_greeting_subscribe_local'), $data['first_name']) . "\n\n";

                    $text .= $this->lang->line('text_thanks_for_subscribing') . "\n\n\n";
                    $text .= sprintf($this->lang->line('text_verification_subscribe_local'), base_url(), $data['verification_code'], $this->language, base_url(), $data['verification_code'], $this->language) . "<br/><br/><br/>";
                    $text .= $this->lang->line('text_message_generated');

                    $message = sprintf($this->lang->line('text_greeting_subscribe_local'), $data['first_name']) . "<br/><br/>";
                    $message .= $this->lang->line('text_thanks_for_subscribing') . "<br/><br/><br/>";

                    $message .= sprintf($this->lang->line('text_verification_subscribe_local'), base_url(), $data['verification_code'], $this->language, base_url(), $data['verification_code'], $this->language) . "<br/><br/><br/>";
                    $message .= $this->lang->line('text_message_generated');

                    $success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);
                    //$success = true;

                    //send to admin	
                    $this->emailmodel->sendmail($to_admin, $from, $bcc, $subject, $message, $text);
                    
                    if ($success === true) {
                        //echo $this->lang->line('text_varification_email') . $data['email'];
						$data['title'] = $this->lang->line('text_title');
						$data['description'] = $this->lang->line('text_description');
						$data['keywords'] = $this->lang->line('text_keywords');
				
						//Text
						$data['text_heading'] = $this->lang->line('heading_title');
							
						$data['breadcrumbs'][] = array(
							'text'      => 'Home',
							'href'      => base_url()
						);
						
						$data['breadcrumbs'][] = array(
							'text'    	=> $this->lang->line('heading_title'),
							'href'     	=> base_url()
						);
						
						$data['alert'] = true;
						$data['success'] = $this->lang->line('text_varification_email') . $data['email'];
                    	
						$data['module'] = "newsletter";
						$data['view_file'] = "varification";
						
						echo Modules::run('template/default_template', $data);
                    } else {
                        echo $this->lang->line('text_an_error_occurred');
                    }
                } else {            
                    $data['title'] = $this->lang->line('text_title');
					$data['description'] = $this->lang->line('text_description');
					$data['keywords'] = $this->lang->line('text_keywords');
			
					//Text
					$data['text_heading'] = $this->lang->line('heading_title');
						
					$data['breadcrumbs'][] = array(
						'text'      => 'Home',
						'href'      => base_url()
					);
					
					$data['breadcrumbs'][] = array(
						'text'    	=> $this->lang->line('heading_title'),
						'href'     	=> base_url()
					);
					
					$data['alert'] = false;
					$data['success'] = $this->lang->line('text_exist_email');
                    
					$data['module'] = "newsletter";
					$data['view_file'] = "varification";
					
					echo Modules::run('template/default_template', $data);
                }
            }
        } else {
        	redirect(base_url(), 'location');
        }
    }

    function verify($verification_code = "", $lang = "english") {
        $data = "";
        //load language
        $this->lang->load('common/header', $this->language);
        $this->lang->load('newsletter/newsletter', $this->language);

        //check verification exists
        $obj_check = $this->subscribermodel->getby_verification_code($verification_code);

        if (!$obj_check) {
            
            $data['title'] = $this->lang->line('text_title');
			$data['description'] = $this->lang->line('text_description');
			$data['keywords'] = $this->lang->line('text_keywords');
	
			//Text
			$data['text_heading'] = $this->lang->line('heading_title');
				
			$data['breadcrumbs'][] = array(
				'text'      => 'Home',
				'href'      => base_url()
			);
			
			$data['breadcrumbs'][] = array(
				'text'    	=> $this->lang->line('heading_title'),
				'href'     	=> base_url()
			);
			
			$data['alert'] = false;
			$data['success'] = $this->lang->line('text_invalid_verfication_code');
                    
			$data['module'] = "newsletter";
			$data['view_file'] = "invarification";
			
			echo Modules::run('template/default_template', $data);
            
        } else {

            $this->subscribermodel->change_verification_subscriber($verification_code, 1); //set verified flag to true, also delete verified records that are more than 24 hours
            //
            $to = $this->emailmodel->get_admin_email(6); // get local newsletter admin email
            //$form = $this->emailmodel->get_admin_email(6);    
            $from = $this->lang->line('text_from_subscribe_local');
            $bcc = "";
            $subject = $this->lang->line('text_newletters_verification');

            $text = $this->lang->line('text_subscribe_updated') . "\n\n";
            $message = $this->lang->line('text_subscribe_updated');

            $to_admin = $to->AdminAddress;
            $this->emailmodel->sendmail($to_admin, $from, $bcc, $subject, $message, $text);

           	$data['title'] = $this->lang->line('text_title');
			$data['description'] = $this->lang->line('text_description');
			$data['keywords'] = $this->lang->line('text_keywords');
	
			//Text
			$data['text_heading'] = $this->lang->line('heading_title');
				
			$data['breadcrumbs'][] = array(
				'text'      => 'Home',
				'href'      => base_url()
			);
			
			$data['breadcrumbs'][] = array(
				'text'    	=> $this->lang->line('heading_title'),
				'href'     	=> base_url()
			);
			
			$data['alert'] = true;
			$data['success'] = sprintf($this->lang->line('text_your_email_has_been_submitted'), $obj_check->First_name, $obj_check->Email);
                    
			$data['module'] = "newsletter";
			$data['view_file'] = "invarification";
			
			echo Modules::run('template/default_template', $data);
        }
    }

}